-- vertical_damage
-- create by panyl
-- 技能: 列伤害。以目标为中心，对其同一列范围敌人造成伤害=魔力 * 威力 * 加成


return {
    apply = function(source, target, skillId, round, para, extra)
        local base = para[1];

        local combine = PropM.combine;
        local PropM_apply = PropM.apply;
        local fetchProps = PropM.fetchProps;
        local trigger = PropM.trigger;

        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, true);

        --最终威力
        local force = math.modf(base * (addon_rate + 1000)/1000);

        -- 2. 攻击方的魔力
        local magic = source:getMagic();

        -- 被对方百分比削弱
        local prop = combine(target, "weak_enemy", "magic");
        magic = PropM_apply(prop, magic);

        -- 被对方绝对值削弱
        prop = combine(target, "weak_enemy2", "magic");
        magic = PropM_apply(prop, magic);

        -- 3. 伤害/计算伤害时魔力+3
        local damage = math.modf(force * (magic + 3) / 1000);

        -- 4. 遍历怪物所在列
        local ret = {};
        local pos = target.dbase:query("pos");
        if not pos then
            pos = target:getOwner();
        end

        local coor = DungeonM.convertToCoor(pos);
        local x;
        local y;
        local m;

        local cover = { };
        for y = 1, DUNGEON_HEIGHT do
            m = DungeonM.convertToPos(coor.x, y);
            table.insert(cover, m);
        end

        -- 天赋起效整行作用
        prop = combine(source, "open_row_damage", skillId);
        if PropM_apply(prop, 1) > 0 or skillId == 236 then      -- 强力地震直接生效，先写死
            for x = 1, DUNGEON_WIDTH do
                m = DungeonM.convertToPos(x, coor.y);
                if m ~= pos then
                    table.insert(cover, m);
                end
            end
        end

        for _, m in pairs(cover) do
            local grid = DungeonM.getGridByPos(m);

            -- 如果是未翻开的格子
            if  not grid:isOpened()  then
                grid:open();
            end

            -- 如果是未死亡的怪物
            if  grid:isMonster() and
                not grid.monster:isDead() then
                local finalDamage = damage;
                if para[2] and tonumber(para[2]) == grid.monster.style then
                    -- 该系别有伤害加成,需重新计算伤害
                    finalDamage = math.modf((force + base * para[4] / 1000) * (magic + 3) / 1000);
                end

                -- 魔法暴击类，多种暴击线性叠加伤害
                local addon_damage = 0;

                -- 伤害类魔法概率增强
                local props = fetchProps(source, "damage_magic");
                for _, prop in ipairs(props) do
                    addon_damage = addon_damage + trigger(source, prop[1], prop[2], finalDamage);
                end
                -- 魔法暴击
                props = fetchProps(source, "critical_magic");
                for _, prop in ipairs(props) do
                    addon_damage = addon_damage + trigger(source, prop[1], prop[2], finalDamage);
                end
                -- 概率双倍
                props = fetchProps(source, "skill_double");
                for _, prop in ipairs(props) do
                    if tonumber(prop[2]) == skillId then
                        addon_damage = addon_damage + trigger(source, prop[1], prop[2], finalDamage);
                    end
                end
                -- 最终伤害
                finalDamage = finalDamage + addon_damage;

                -- 魔法抗性
                prop = combine(grid.monster, "resist", "magic");
                -- 忽视抗性
                local p = combine(source, "ignore_resist", skillId);
                prop[3] = PropM_apply(p, prop[3]);
                p = combine(source, "ignore_resist", "magic");
                prop[3] = PropM_apply(p, prop[3]);

                finalDamage = PropM_apply(prop, finalDamage);

                -- 增加命中动作
                if SkillM.getSequence(grid.monster) == nil then
                    SkillM.getSequenceList()[grid.monster] = CastSequence.create(source, grid.monster, skillId);
                end

                SkillM.getSequence(grid.monster):hit(source, grid.monster, skillId);

                -- 受创
                CombatM.receiveDamage(source, grid.monster, finalDamage, skillId, extra);

                -- 目标
                table.insert(ret, grid.monster);
            end
        end

        -- 返回作用目标
        return ret;
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        if #arr == 1 then
            return { tonumber(arr[1]), };
        else
            return { tonumber(arr[1]), tonumber(arr[2]), tonumber(arr[3]), };
        end
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        local base = para[1];

        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, false);

        --最终威力
        local force = math.modf(base * (addon_rate + 1000)/1000);

        -- 2. 攻击方的魔力
        local magic = source:getMagic();

        -- 3. 伤害
        local damageBase = math.modf(base * (magic + 3) / 1000);
        local damageForce = math.modf(force * (magic + 3) / 1000);
        local damageAddon = damageForce - damageBase;
        if damageAddon == 0 then
            desc = string.gsub(desc, "{vertical_damage}", damageForce);
        else
            desc = string.gsub(desc, "{vertical_damage}", damageBase .. "(+" .. damageAddon .. ")");
        end

        -- 系别TODO

        return desc;
    end,
}
